# -*- coding:utf-8 -*-
import json

from flask import Blueprint, jsonify, request

from config import db
from dbmodel.bank_trend import BankTrend
from dbmodel.city_count import CityCount
from dbmodel.company_amont import CompanyAmont
from dbmodel.company_sorted import CompanySorted
from dbmodel.economic_active import EconomicActive
from dbmodel.every_regions_ershoufang_count import EveryRegionsErshoufangCount
from dbmodel.every_regions_zufang_count import EveryRegionsZufangCount
from dbmodel.fang_wu_zheng_shou import FangWuZhengShou
from dbmodel.gong_zu_fang import GongZuFang
from dbmodel.serven_area import ServenArea
from dbmodel.yu_shou_trend import YuShouTrend
from dbmodel.zufang_shoufang_price_compare import ZufangShoufangPriceCompare

"""
本视图专门用于处理ajax数据
"""
data = Blueprint('data', __name__)


@data.route('/getMap', methods=['GET'])
def get_map():
    data = db.session.query(CityCount).all()
    view_data = {}
    view_data["series"] = []

    def build_view_data(item):
        dic = {}
        dic["value"] = item.count
        dic["name"] = item.city
        view_data["series"].append([dic])

    [build_view_data(item) for item in data]

    return json.dumps(view_data, ensure_ascii=False)


@data.route('/getServenArea', methods=['GET'])
def get_serven_area():
    data = db.session.query(ServenArea).all()
    view_data = {}
    view_data["series_data"] = []

    def build_view_data(item):
        tmp_dic = {}
        tmp_dic["price"] = item.price
        tmp_dic["city"] = item.city
        view_data["series_data"].append(tmp_dic)

    [build_view_data(item) for item in data]

    return json.dumps(view_data, ensure_ascii=False)


@data.route('/getZufangShoufangPriceCompare', methods=['GET'])
def get_zufang_shoufang_price_compare():
    data = db.session.query(ZufangShoufangPriceCompare).all()
    view_data = {}
    view_data["xAxis"] = ["华北", "华东", "东北", "华中", "华南", "西南", "西北"]
    view_data["series1"] = []
    view_data["series2"] = []
    all_area = ["华北", "华东", "东北", "华中", "华南", "西南", "西北"]

    def build_view_data(item):
        tmp_dic = {}
        tmp_dic["price"] = item.price
        tmp_dic["city"] = item.city
        if item.data_type == "zufang":
            view_data["series1"].append(item.price)
        else:
            view_data["series2"].append(item.price)

    [build_view_data(item) for i in all_area for item in data if item.city == i]

    return json.dumps(view_data, ensure_ascii=False)


@data.route('/getEveryRegionsZufangCount', methods=['GET'])
def get_every_regions_zufang_count():
    data = db.session.query(EveryRegionsZufangCount).all()
    view_data = {}
    view_data["xAxis"] = ["华北", "华东", "东北", "华中", "华南", "西南", "西北"]
    view_data["series1"] = []
    all_area = ["华北", "华东", "东北", "华中", "华南", "西南", "西北"]

    def build_view_data(item):
        tmp_dic = {}
        tmp_dic["value"] = item.count
        tmp_dic["name"] = item.city
        view_data["series1"].append(tmp_dic)

    [build_view_data(item) for i in all_area for item in data if item.city == i]

    return json.dumps(view_data, ensure_ascii=False)


@data.route('/getEveryRegionsErshoufangCount', methods=['GET'])
def get_every_regions_ershoufang_count():
    data = db.session.query(EveryRegionsErshoufangCount).all()
    view_data = {}
    view_data["xAxis"] = ["华北", "华东", "东北", "华中", "华南", "西南", "西北"]
    view_data["series1"] = []
    all_area = ["华北", "华东", "东北", "华中", "华南", "西南", "西北"]

    def build_view_data(item):
        tmp_dic = {}
        tmp_dic["value"] = item.count
        tmp_dic["name"] = item.city
        view_data["series1"].append(tmp_dic)

    [build_view_data(item) for i in all_area for item in data if item.city == i]

    return json.dumps(view_data, ensure_ascii=False)


@data.route('/getEconomicActive', methods=['GET'])
def get_economic_active():
    data = db.session.query(EconomicActive).all()
    view_data = {}
    view_data["series1"] = []

    def build_view_data(item):
        view_data["series1"].append(item.city)

    [build_view_data(item) for item in data]

    return json.dumps(view_data, ensure_ascii=False)


@data.route('/getFangWuZhengShou', methods=['GET'])
def get_fang_wu_zheng_shou():
    data = db.session.query(FangWuZhengShou).all()
    view_data = {}
    view_data["series1"] = []

    def build_view_data(item):
        tmp_dic = {}
        tmp_dic["name"] = item.area
        tmp_dic["value"] = item.counter
        view_data["series1"].append(tmp_dic)

    [build_view_data(item) for item in data]

    return json.dumps(view_data, ensure_ascii=False)


@data.route('/getYuShouTrend', methods=['GET'])
def get_yu_shou_trend():
    data = db.session.query(YuShouTrend).order_by(YuShouTrend.year.asc()).all()
    view_data = {}
    view_data["year"] = []
    view_data["area"] = []

    def build_view_data(item):
        view_data["area"].append(item.area)
        view_data["year"].append(item.year)

    [build_view_data(item) for item in data]

    return json.dumps(view_data, ensure_ascii=False)


@data.route('/getGongZuFang', methods=['GET'])
def get_gong_zu_fang():
    data = db.session.query(GongZuFang).all()
    view_data = {}
    view_data["xAxis"] = []
    view_data["series1"] = []
    view_data["series2"] = []

    def build_view_data(item):
        view_data["xAxis"].append(item.location)
        view_data["series1"].append(item.counter)
        view_data["series2"].append(item.area)

    [build_view_data(item) for item in data]

    return json.dumps(view_data, ensure_ascii=False)


@data.route('/getCompanySorted', methods=['GET'])
def get_company_sorted():
    order = request.args.get("order")
    if order == "1":
        data = db.session.query(CompanySorted).order_by(CompanySorted.score.desc()).all()[0:5]
    else:
        data = db.session.query(CompanySorted).order_by(CompanySorted.score.asc()).all()[0:5]

    view_data = {}
    view_data["series"] = []

    def build_view_data(item):
        view_data["series"].append({"value": item.score, "name": item.city})

    [build_view_data(item) for item in data]

    return json.dumps(view_data, ensure_ascii=False)


@data.route('/getBankTrend', methods=['GET'])
def get_bank_trend():
    data = db.session.query(BankTrend).order_by(BankTrend.year.asc()).all()
    view_data = {}
    view_data["year"] = []
    view_data["amount"] = []

    def build_view_data(item):
        view_data["year"].append(item.year)
        view_data["amount"].append(item.amount)

    [build_view_data(item) for item in data]

    return json.dumps(view_data, ensure_ascii=False)


@data.route('/getCompanyAmont', methods=['GET'])
def get_company_amont():
    data = db.session.query(CompanyAmont).all()
    view_data = {}
    view_data["series"] = []

    def build_view_data(item):
        dic = {}
        dic["value"] = item.amount
        dic["name"] = item.company_name
        view_data["series"].append(dic)

    [build_view_data(item) for item in data]

    return json.dumps(view_data, ensure_ascii=False)
